﻿using System;
using System.Data;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Openfree.Framework.DataAccess;

public partial class PMSWeb_ProjectStartManager : PMSBase
{
    private SqlCodeConfig codeConfig;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.TableName = "PMS_Project";
        this.Key = "ProjectID";
        if (!IsPostBack)
        {
            pnlGrid.Visible = true;
            pnlEdit.Visible = false;
            string cmdText = string.Format("SELECT T.*,A.CodeName AS InvestKindName FROM {0} T LEFT JOIN " + SqlCodeConfig.TableName + " A ON A.CodeValue=T.InvestKind AND A.Category='" + PMSCodeCategory.PMS_P_TZLB + "' WHERE T.ProjectState='" + PMSCodeCategory .PMS_P_LiXiang+ "' ORDER BY T.CreateTime DESC", this.TableName);
            FillGirdView(0, cmdText);
            InitData();
        }
        else
        {
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
        }
    }
    public void InitData()
    {
        codeConfig = new SqlCodeConfig(this.Connection);
        DataSet ds = codeConfig.LoadDataByCategory(PMSCodeCategory.PMS_P_TZLB);
        this.BindDropDownList(S_InvestKind, ds, true);
        this.BindDropDownList(P_InvestKind, ds, true);
        ds = codeConfig.LoadDataByCategory(PMSCodeCategory.PMS_P_ZT);
        this.BindDropDownList(S_ProjectState, ds, true);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        StringBuilder sql = new StringBuilder(string.Format("SELECT T.*,A.CodeName AS InvestKindName FROM {0} T LEFT JOIN " + SqlCodeConfig.TableName + " A ON A.CodeValue=T.InvestKind AND A.Category='" + PMSCodeCategory.PMS_P_TZLB + "' WHERE 1=1 and T.ProjectState='" + PMSCodeCategory.PMS_P_LiXiang + "'", this.TableName));
        if (!string.IsNullOrEmpty(S_ProjectCode.Text))
        {
            sql.Append(" AND T.ProjectCode like '%").Append(S_ProjectCode.Text).Append("%'");
        }
        if (!string.IsNullOrEmpty(S_ProjectName.Text))
        {
            sql.Append(" AND T.ProjectName like '%").Append(S_ProjectName.Text).Append("%'");
        }
        if (!string.IsNullOrEmpty(S_ProjectCreater.Text))
        {
            sql.Append(" AND T.ProjectCreater like '%").Append(S_ProjectCreater.Text).Append("%'");
        }
        if (!string.IsNullOrEmpty(S_InvestKind.SelectedValue))
        {
            sql.Append(" AND T.InvestKind = '").Append(S_InvestKind.SelectedValue).Append("'");
        }
        if (!string.IsNullOrEmpty(S_ProjectState.SelectedValue))
        {
            sql.Append(" AND T.ProjectState = '").Append(S_ProjectState.SelectedValue).Append("'");
        }
        sql.Append(" ORDER BY T.CreateTime DESC");
        FillGirdView(0, sql.ToString());
    }
    protected void FillGirdView(int currentPage, string cmdText)
    {
        if (string.IsNullOrEmpty(cmdText))
        {
            cmdText = ViewState[this.ClientID + "cmdText"].ToString();
        }
        SqlSplitPage splitPage = new SqlSplitPage(base.Connection, cmdText);
        splitPage.PageSize = 10;
        DataSet ds = splitPage.LoadDataSet(currentPage);
        //成功查询出数据后缓存
        ViewState[this.ClientID + "cmdText"] = cmdText;
        gridView.ShowFooter = true;
        gridView.NavigationData = splitPage.GetNavigationData(currentPage);
        gridView.DataSource = ds.Tables[0];
        gridView.DataBind();
    }

    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[7].Text = PMSCodeCategory.GetProjectStateName(e.Row.Cells[7].Text);
        }
    }

    protected void gridView_SplitPageIndexChange(object sender, CommandEventArgs e)
    {
        FillGirdView(Int32.Parse(e.CommandArgument.ToString()), ViewState[this.ClientID + "cmdText"].ToString());
    }


    private void ResetComponent()
    {
        this.ClearPrexControlValues(base.DefaultPrex);
        hfProjectID.Value = string.Empty;
    }


    protected void btnAdd_Click(object sender, EventArgs e)
    {
        hfAddOrEdit.Value = "Add";
        hfProjectID.Value = string.Empty;
        pnlGrid.Visible = false;
        pnlEdit.Visible = true;
        ResetComponent();
        P_AcceptDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
    }

    protected void btnRemove_Click(object sender, EventArgs e)
    {
        string id = Request.Form["ProjectID"];
        if (string.IsNullOrEmpty(id))
        {
            base.Alert("请选择一条记录");
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
            return;
        }
        base.WriteLog("项目", "删除项目'" + id + "'!",
            LogType.Warning, this.GetType().ToString());
        PMSAccess.ExecuteNonQuery(this.Connection,"update PMS_Project set ProjectState='"+PMSCodeCategory.PMS_P_ZuoFei+"' where ProjectID='" + id + "'");
        //PMSAccess.RemoveDataRow(this.TableName, this.Key, Request["ProjectID"].ToString());
        FillGirdView(this.gridView.SplitPageIndex, string.Empty);
    }


    protected void btnEdit_Click(object sender, EventArgs e)
    {
        string id = Request.Form["ProjectID"];
        if (string.IsNullOrEmpty(id))
        {
            base.Alert("请选择一条记录");
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
            return;
        }
        ResetComponent();
        hfAddOrEdit.Value = "Edit";
        hfProjectID.Value = id;
        pnlGrid.Visible = false;
        pnlEdit.Visible = true;
        DataSet ds = PMSAccess.GetDataSet(this.TableName, this.Key, id);
        DataRow dr = ds.Tables[0].Rows[0];
        base.FillValueByPrexFromDataRow(dr, this.DefaultPrex);
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlGrid.Visible = true;
        pnlEdit.Visible = false;
        FillGirdView(this.gridView.SplitPageIndex, string.Empty);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (hfAddOrEdit.Value == "Add")
        {
            ExecuteAdd();
        }
        else
        {
            ExecuteEdit();
        }
    }

    private void ExecuteAdd()
    {
        DataSet ds = PMSAccess.GetEmptyDataSet(this.TableName);
        DataRow dr = ds.Tables[0].NewRow();
        dr["ProjectID"] = System.Guid.NewGuid().ToString();
        this.FillDataRowByPrex(ref dr, this.DefaultPrex, EnumOperator.ADD);
        PMSAccess.AppendValue(ref dr, EnumOperator.ADD);
        dr["ProjectState"] = PMSCodeCategory.PMS_P_LiXiang;
        ds.Tables[0].Rows.Add(dr);
        PMSAccess.UpdateDataSet(this.TableName, ds.GetChanges());
        ResetComponent();
        base.Alert("新建成功");
    }


    private void ExecuteEdit()
    {
        DataSet ds = PMSAccess.GetDataSet(this.TableName, this.Key, hfProjectID.Value);
        DataRow dr = ds.Tables[0].Rows[0];
        this.FillDataRowByPrex(ref dr, this.DefaultPrex, EnumOperator.EDIT);
        PMSAccess.UpdateDataSet(this.TableName, ds.GetChanges());
        base.Alert("修改成功");
    }
}
